package com.example.zg.zg_312_ordering_new.fragments;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zg.zg_312_ordering_new.R;


public class CollectFragment extends Fragment {

    private ViewPager viewPager;
    private PagerSlidingTabStrip pagerSlidingTabStrip;
    private CollectFragment_shop collectFragment_shop =null;
    private CollectFragment_food collectFragment_food =null;

    public CollectFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_collection, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);

        setpagerstyle();//设置PagerSlidingTabStrip显示效果

        viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        pagerSlidingTabStrip.setViewPager(viewPager);
    }

    private void setpagerstyle() {
        pagerSlidingTabStrip.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        pagerSlidingTabStrip.setTabBackground(0); // 取消点击Tab时的背景色
    }

    private void init(View view) {
        viewPager = view.findViewById(R.id.viewPager2);
        pagerSlidingTabStrip = view.findViewById(R.id.pagerslidingtabstrip);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        private String[] titles = {"店铺", "菜品"};//显示在二级导航上的标题文字

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];//确定当页导航上文字
        }

        @Override
        public Fragment getItem(int i) {//根据位置返回具体某个导航上对应的Fragment
            switch (i){
                case 0:
                    if(collectFragment_shop ==null)
                        collectFragment_shop = new CollectFragment_shop();
                    return collectFragment_shop;

                case 1:
                    if(collectFragment_food ==null)
                        collectFragment_food = new CollectFragment_food();
                    return collectFragment_food;

                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return titles.length;//二级导航个数
        }
    }
}
